<?php
/**
 * Created by PhpStorm.
 * User: zhanglu
 * Date: 2019/6/21
 * Time: 5:25 PM
 */

namespace frontend\controllers;

use Yii;
use yii\web\Controller;
use common\components\MobileDetect;
use yii\data\Pagination;

class BaseController extends Controller
{

    public $pageSize = 10;

    public function mobileDetect($route){
        $detect = new MobileDetect();
        if($detect->isMobile()||$detect->isTablet()){
            return $this->redirect($route);
        }
    }

    public function pcDetect($route){
        $detect = new MobileDetect();
        if(!$detect->isMobile()&&!$detect->isTablet()){
            return $this->redirect($route);
        }
    }

    public function pagination($query){
        $total_count = $query->count();
        $pages = new Pagination(['totalCount' =>$total_count, 'pageSize' => $this->pageSize]);
        $list = $query->offset($pages->offset)->limit($pages->limit)->all();
        return [
            'pages' => $pages,
            'list' => $list,
            'total_count' => $total_count,
        ];
    }

}